package com.zzy.jkb.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzy.jkb.api.CommonResult;
import com.zzy.jkb.entity.ScanHistory;
import com.zzy.jkb.entity.VaccineHistory;
import com.zzy.jkb.service.IScanHistoryService;
import com.zzy.jkb.vo.ScanHistoryVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 扫码记录 前端控制器
 * </p>
 *
 * @author zzy
 * @since 2023-02-03
 */
@Controller
@RequestMapping("/jkb/scanHistory")
public class ScanHistoryController {


    @Autowired
    IScanHistoryService iScanHistoryService;

    @RequestMapping(path = "/list", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询扫码记录")
    public CommonResult<List<ScanHistoryVo>> list(@RequestParam String userId) {
        List<ScanHistoryVo> scanHistory = iScanHistoryService.getScanHistory(userId);
        List<ScanHistoryVo> collect = scanHistory.stream().map(item -> {
            String identityNo = item.getIdentityNo();
            //身份证号脱敏处理
            if (item != null && StringUtils.isNotBlank(identityNo)) {
                if (identityNo.length() == 15) {
                    identityNo = identityNo.replaceAll("(\\w{6})\\w*(\\w{3})", "$1******$2");
                }
                if (identityNo.length() == 18) {
                    identityNo = identityNo.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2");
                }
            }
            item.setIdentityNo(identityNo);
            return item;
        }).collect(Collectors.toList());
        return CommonResult.success(collect);
    }

    @RequestMapping(path = "/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("扫码")
    public CommonResult save(@RequestBody ScanHistory scanHistory) {
        scanHistory.setCreateBy("xiangge");
        scanHistory.setCreateTime(LocalDateTime.now());
        return CommonResult.success(iScanHistoryService.save(scanHistory));
    }


}
